package welsir.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import welsir.entity.dos.ArticleComment;
import welsir.entity.vo.ResultMessage;
import welsir.mapper.ArticleCommentMapper;
import welsir.security.annotations.Authentication;
import welsir.security.enums.UserEnums;
import welsir.service.IArticleCommentService;
import welsir.util.ResultUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Genius
 * @date 2023/03/07 22:51
 * @admin 需要管理员权限
 **/
@RestController
@RequestMapping("/articleComment")
@Api(value = "文章评论Controller",tags = {"文章评论接口"})
public class ArticleCommentController {

    @Resource
    private ArticleCommentMapper articleCommentMapper;

    @Autowired
    private IArticleCommentService articleCommentService;

    /**
     * 根据文章id获取全部评论
     * @param articleId
     * @return
     */
    @ApiOperation(value = "根据文章uid获取全部评论")
    @Authentication(role = UserEnums.ADMIN)
    @GetMapping("/getAllArticleComment/{articleId}")
    public ResultMessage<List<ArticleComment>> getAllArticleCommentById(
            @PathVariable("articleId") Integer articleId
    ) {

        QueryWrapper<ArticleComment> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id",articleId);

        return ResultUtil.data(articleCommentMapper.selectList(wrapper));
    }

    /**
     * 根据时间排序获取全部评论
     * @return
     */
    @ApiOperation(value = "根据时间获取全部评论")
    @Authentication(role = UserEnums.ADMIN)
    @GetMapping("/getAllArticleComments")
    public ResultMessage<List<ArticleComment>> getAllArticleComments(
    ) {
        QueryWrapper<ArticleComment> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("add_time");
        return ResultUtil.data(articleCommentMapper.selectList(wrapper));
    }

   /**
        * 添加评论
        * @param articleComment
        * @return
    */
   @ApiOperation(value = "添加评论")
   @Authentication(role = UserEnums.ADMIN)
   @PostMapping("/addArticleComment")
    public ResultMessage<Integer> addArticleComment(@RequestBody ArticleComment articleComment) {
        articleCommentMapper.insert(articleComment);
        return ResultUtil.success();
    }

    /**
     * 根据文章ID查看文章评论数
     * @param articleId
     * @return
     */
    @ApiOperation(value = "根据文章id查看文章评论数")
    @Authentication(role = UserEnums.ADMIN)
    @GetMapping("/getArticleCommentNum/{articleId}")
    public ResultMessage<Integer> getArticleCommentNum(
            @PathVariable("articleId") Integer articleId
    ) {
        QueryWrapper<ArticleComment> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id",articleId);
        return ResultUtil.data(articleCommentMapper.selectCount(wrapper));
    }

    @Authentication(role = UserEnums.ADMIN)
    @ApiOperation(value = "根据评论id删除评论")
    @GetMapping("/deleteArticleComment/{id}")
    public ResultMessage<Object> deleteArticleCommentById(@PathVariable Integer id){
        return ResultUtil.data(articleCommentMapper.delete(new QueryWrapper<ArticleComment>().eq("id",id)));
    }

    @Authentication(role = UserEnums.ADMIN)
    @ApiOperation(value = "更新评论")
    @PostMapping("/updateArticleComment")
    public ResultMessage<ArticleComment> updateArticleComment(@RequestBody ArticleComment articleComment){
        articleCommentMapper.updateById(articleComment);
        return ResultUtil.data(articleComment);
    }

}
